extends KinematicBody2D
class_name Enemy

export (int) var EnemyWalkSpeed
export (int) var EnemyRunSpeed

var velocity = Vector2(1,0)

var screensize
# Called when the node enters the scene tree for the first time.
func _ready():
	screensize = get_viewport().size
	CommonSignals.connect("EnemyFoundPlayer",self,"_on_EnemyFoundPlayer")
	CommonSignals.connect("EnemyIgnorePlayer",self,"_on_EnemyIgnorePlayer")
	$AnimationPlayer.play("Walk")


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	
#	position += velocity * delta
	var collision = move_and_collide(velocity * delta)
	if collision != null:
		var body 
		body = collision.collider
		if(body.name=="Player"):
			CommonSignals.emit_signal("EnemyCaughtPlayer")
	else:
		position.x = clamp(position.x,0,screensize.x)
		position.y = clamp(position.y,0,screensize.y)
#	position.x = clamp(position.x,0,screensize.x)
#	position.y = clamp(position.y,0,screensize.y)


func _on_FoundBody_body_entered(body):
	CommonSignals.emit_signal("EnemyFoundPlayer")

#如果看到了铁汁，切换到Catch态
func _on_EnemyFoundPlayer():
	$State_Machine.transition_to("EnemyCatch")

	


func _on_FoundBody_body_exited(body):
	CommonSignals.emit_signal("EnemyIgnorePlayer")

#铁汁走出视线范围，继续巡逻
func _on_EnemyIgnorePlayer():
	$State_Machine.transition_to("EnemyRoutine")


